<template>
  <p style="height: 35px">
    <Button type="primary" :loading="loading" @click="save()">
      <span v-if="!loading">保存</span>
      <span v-else>保存...</span>
    </Button>
    <Button style="margin-left: 10px" @click="back()">取消</Button>
    <slot></slot>
  </p>
</template>
<script>

  export default {
    name: 'abSave',
    props: {
      'url':{required: true}, // 保存数据请求的url
      'model':{required: true}, // 保存的数据
      "method":{default:"post"}, // 提交方式 默认post 可以改为 postForm
      "beforeSave":{type:"Function"}, // 保存前置js
      "backRouter":{required: false} // 成功后跳转页,默认前一页
    },
    data() {
      return {
        loading:false
      }
    },
    methods: {
      save() {
        this.loading = true;
        
        // doValidate
        let self = this;
    	let rtn = this.abHttpUtil[this.method](this.url, this.model);
    	
        rtn.then(function(data) {
          if(data.isOk) {
            if(self.afterSave) {
              return self.afterSave(data);
            }
            self.loading = false;
            self.back();
          } else {
          	self.$message.error(data.msg);
          }
        },function(errorCode) {
      	  self.$message.error("请求失败!" + errorCode);
        });
      },
      back(){
    	  if(!this.backRouter){
    		  console.error("backRouter 未配置！");
          	return; 
          }
    	  this.$router.push({
              name: this.backRouter
          })
      }
    }
  }
</script>
